<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>发布房产信息</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #e9ecef;
            margin: 0;
            padding: 0;
            color: #343a40;
        }
        .container {
            width: 60%;
            margin: 0 auto;
            background-color: #ffffff;
            padding: 30px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            border-radius: 10px;
            margin-top: 50px;
        }
        h1 {
            text-align: center;
            color: #495057;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #495057;
            font-weight: 500;
        }
        input[type="text"], input[type="number"], select {
            width: 100%;
            padding: 10px;
            box-sizing: border-box;
            border: 1px solid #ced4da;
            border-radius: 5px;
            background-color: #f8f9fa;
            color: #495057;
            height:50px;
            font-size: 22px;
        }
        input[type="text"]:focus, input[type="number"]:focus, select:focus {
            border-color: #80bdff;
            outline: none;
            background-color: #ffffff;
        }
        button {
            display: inline-block;
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .success-message {
            display: none;
            color: green;
            margin-top: 20px;
            text-align: center;
        }
        .image-container {
            position: relative;
            display: flex;
            margin-bottom: 20px;
            text-align: center;
        }
        #avatar {
            width: 100px;
            height: 100px;
            object-fit: cover;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
            display: block;
            margin: 0 auto;
            border-radius: 5px;
            margin-left: 25px;
        }
        .hover-text {
            position: absolute;
            top: 80%;
            left: 13%;
            transform: translate(-50%, -50%);
            background-color: rgba(0, 0, 0, 0.7);
            color: white;
            padding: 5px 10px;
            border-radius: 5px;
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 14px;
            width: 80px;
            text-align: center;
            cursor: pointer;
        }
        .image-container:hover .hover-text {
            opacity: 1;
            cursor: pointer;
        }
        #fileInput {
            display: none;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>我是包租公，你的好房东</h1>

    <form id="propertyForm" style="font-size: 20px">
        <input type="hidden" id="imageUrl" name="imageUrl">
        <div class="form-group">
            <label for="title">标题</label>
            <input type="text" id="title" name="title" required>
        </div>

        <div class="image-container">
            <div style="position: relative;top:20px;">
                <label style="position: relative;top:20px ">上传图片</label>
            </div>
            <div>
                <img id="avatar" src="/icon/添加图片.png" alt="房子图片">
                <div class="hover-text">添加图片</div>
                <input type="file" id="fileInput" accept="image/*">
            </div>
        </div>

        <div class="form-group">
            <label for="description">描述</label>
            <input type="text" id="description" name="description" required>
        </div>
        <div class="form-group">
            <label for="province">省</label>
            <input type="text" id="province" name="province" required>
        </div>
        <div class="form-group">
            <label for="city">市</label>
            <input type="text" id="city" name="city" required>
        </div>
        <div class="form-group">
            <label for="community">小区</label>
            <input type="text" id="community" name="community" required>
        </div>
        <div class="form-group">
            <label for="type">类型</label>
            <select id="type" name="type" required>
                <option value="新房">新房</option>
                <option value="二手房">二手房</option>
            </select>
        </div>
        <div class="form-group">
            <label for="houseType">房屋类型</label>
            <input type="text" id="houseType" name="houseType" required>
        </div>
        <div class="form-group">
            <label for="area">面积 (平方米)</label>
            <input type="number" id="area" name="area" required>
        </div>
        <div class="form-group">
            <label for="price">价格 (元)</label>
            <input type="number" id="price" name="price" required>
        </div>
        <button style="margin-left: 37%;width:220px;margin-top: 20px;height:50px;font-size:22px;letter-spacing:5px" type="submit">发布房产</button>
    </form>
</div>

<script>
    const avatar = document.getElementById('avatar');
    const fileInput = document.getElementById('fileInput');
    const imageUrlInput = document.getElementById('imageUrl');

    avatar.addEventListener('click', () => {
        fileInput.click();
    });
    const token = localStorage.getItem('token');

    fileInput.addEventListener('change', () => {
        const file = fileInput.files[0];
        if (file) {
            const formData = new FormData();
            formData.append('image', file);

            fetch('http://localhost:8080/upload1', {
                method: 'POST',
                headers: {
                    'Authorization': `Bearer ${token}`
                },
                body: formData
            })
                .then(response => response.json())
                .then(data => {
                    if (data.code === 1) {
                        avatar.src = data.data; // 更新图片URL
                        imageUrlInput.value = data.data; // 保存头像URL到隐藏输入字段

                    } else {
                        alert('上传失败');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('上传出错');
                });
        }
    });

    document.getElementById('propertyForm').addEventListener('submit', function(event) {
        event.preventDefault();

        const landlordId = localStorage.getItem('userId');
        if (!landlordId) {
            alert('未找到房东ID，请重新登录');
            return;
        }

        const type = document.getElementById('type').value === '新房' ? 1 : 0;

        const propertyData = {
            title: document.getElementById('title').value,
            description: document.getElementById('description').value,
            province: document.getElementById('province').value,
            city: document.getElementById('city').value,
            community: document.getElementById('community').value,
            type: type,
            houseType: document.getElementById('houseType').value,
            area: parseInt(document.getElementById('area').value),
            price: parseInt(document.getElementById('price').value),
            landlordId: parseInt(landlordId),
            imageUrl: document.getElementById('imageUrl').value // 包含头像URL
        };

        fetch('http://localhost:8080/user/saveMyPostProperty', {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${token}`, // Include the token in the Authorization header
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(propertyData)
        })
            .then(response => response.json())
            .then(data => {
                if (data.code === 1) {
                    alert('发布成功！');
                } else {
                    alert('保存失败：' + data.msg);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('保存失败，请稍后重试');
            });
    });
</script>
</body>
</html>